package main

import (
	"fmt"
	"github.com/nick/vdamtools/apps/dumpserver"
	"os"
	"time"

	"github.com/nick/vdamtools/apps/call"
	"github.com/nick/vdamtools/apps/config"
	"github.com/nick/vdamtools/apps/dashboard"
	"github.com/nick/vdamtools/apps/dmdemo"
	"github.com/nick/vdamtools/apps/monitor"
	"github.com/nick/vdamtools/pkg/loggerv"
	"go.uber.org/zap"
)

func main() {
	cmd := start()
	switch cmd {
	case "dashboard":
		dashboard.Start()
	case "agent": //nohup ./tools log > logs/tools_output.log 2>&1 &
		monitor.Run()
	case "test": // nohup ./tools log > logs/tools_output.log 2>&1 &
		call.Start() // ps -ef | grep ./tools | grep -v rmq | grep -v grep  | awk '{print $2}' | xargs -r kill -9
	case "data": // chmod +x vtools && nohup ./vtools data > output.log & tail -f output.log
		dm := dmdemo.AddDataTest(10, 1000, 100) // ps -ef | grep vtools  | awk '{print $2}' | xargs kill -9
		dm.Run()
	case "dumpsrver":
		ds := dumpserver.AddDumpServer()
		ds.Run()
	default:
		help()
	}
	loggerv.Logger.Info("工具执行完毕退出执行.", zap.Time("now", time.Now()))
}

func help() {
	fmt.Println("vtools [cmd] <parameters>")
	fmt.Println("vtools help                           -  1.帮助命令")
	fmt.Println("vtools dashboard                      -  2. 监控仪表盘")
	fmt.Println("vtools agent -file vtools1.yaml     -  3. 监控agent,需要配置不同的server.hostname")
	fmt.Println("-p 配置连接池大小")
	os.Exit(0)
}

func start() string {
	args := os.Args[1:]
	if len(args) < 1 {
		help()
	}
	command := args[0]
	file := "vtools.yaml"

	for i := 1; i < len(args); i++ {
		switch args[i] {
		case "-file":
			if i+1 < len(args) {
				file = args[i+1]
				i += 1
			} else {
				help()
			}
		default:
			help()
		}
	}

	loggerv.Config(1024, 3, 1000, "logs", "vtools", false) //配置日志文件
	loggerv.Logger.Info("read config file:", zap.Any("file", file))
	config.ReadConfig(file) //读取配置文件
	loggerv.Logger.Info("confirm config:", zap.Any("GlobalConfig", config.GlobalConfig))

	loggerv.Logger.Info("Tools start .", zap.Int64("Port", config.GlobalConfig.Server.Port))
	return command
}

//fmt.Println("tools delete                      -  2.删除时序数据库数据")
//fmt.Println("tools data   -t 100 -d 1000 -m 70 -  3.生成时序数据库数据，100个线程生成1000条测试数据到时序数据库")
//fmt.Println("tools deletedm                    -  4.删除达梦数据库数据")
//fmt.Println("tools datadm -t 100 -d 1000 -m 70 -  5.生成达梦数据库数据，100个线程生成1000条测试数据到时序数据库")
